<?php

namespace App\Http\Middleware;
use App\Http\Controllers\FunctionController;
use App\Http\Controllers\UploadController;
use App\Http\Controllers\WxController;
use App\Models\User;
use App\Utils\CodeUtil;
use Closure;
use Cookie;

class WxLogin {
	/**
	 * Handle an incoming request.
	 * 不分离的微信登陆
	 * @param  \Illuminate\Http\Request $request
	 * @param  \Closure $next
	 * @return mixed
	 */
	public function handle($request, Closure $next) {
        // setcookie("username",1111,time()+3600*24*365);
        //var_dump($_COOKIE);
		if (!empty($_COOKIE['username'])) {
            return $next($request);
        } else {
            $user = new User();
            $user->name = FunctionController::getRandom(32);
            $user->email = $user->name;
            $user->realname =  FunctionController::randName();
            $user->create();
            setcookie("username",$user->id,time()+3600*24*365);
            return $next($request);
        }
	}
}
